/*
 * Copyright (c) 2008-2009 Ravenhearte Design
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'Ravenhearte Design' nor the names of its contributors 
 *   may be used to endorse or promote products derived from this software 
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.ravenhearte.hardboild.gamestates;

import java.util.HashMap;
import java.util.Iterator;

import com.jme.scene.Spatial;
import com.jme.scene.Spatial.CullHint;
import com.jme.renderer.Renderer;
import com.jme.renderer.ColorRGBA;
import com.jme.system.DisplaySystem;
import com.jmex.game.state.GameState;
import com.jmex.game.state.GameStateManager;

import org.ravenhearte.hardboild.app.*;
import org.ravenhearte.hardboild.gui.*;
import org.ravenhearte.hardboild.gui.Element.Alignment;

public class MainMenu extends GameState implements InputState{
	private Container guiRoot, defaultMenu;
	private HashMap<String, Button> buttons;
	
	public MainMenu() {
		name = "Main Menu";
		
		buttons = new HashMap<String, Button>();
		
		guiRoot = new Container("GUI Root", 0, 0, 1024, 768);
		guiRoot.setBackground("titlescreen.png", 0, 0);
		guiRoot.toggleManagedLayout();
		
		guiRoot.addElement(constructDefaultMenu());
		
        guiRoot.getSpatial().setRenderQueueMode(Renderer.QUEUE_ORTHO);
        guiRoot.getSpatial().setLightCombineMode(Spatial.LightCombineMode.Off);
		guiRoot.getSpatial().updateRenderState();
	}
	
	public Container constructDefaultMenu() {
		defaultMenu = new Container("Default Menu", 700, 30, 300, 300);
		
		defaultMenu.setBorderedBackground("titlescreen.png", 0, 768);

		TextButton temp = new TextButton("New Game", ColorRGBA.black, 40);
		temp.setAlignment(Alignment.CENTERED);
		temp.print("New Game");
		temp.setState("MouseOut", ColorRGBA.black);
		temp.setState("MouseOver", ColorRGBA.white);
		temp.setState("MouseClick", ColorRGBA.gray);
		buttons.put(temp.getName(), temp);
		defaultMenu.addElement(temp);
		
		temp = new TextButton("Load Game", ColorRGBA.black, 40);
		temp.setAlignment(Alignment.CENTERED);
		temp.print("Load Game");
		temp.setState("MouseOut", ColorRGBA.black);
		temp.setState("MouseOver", ColorRGBA.white);
		temp.setState("MouseClick", ColorRGBA.gray);
		buttons.put(temp.getName(), temp);
		defaultMenu.addElement(temp);
		
		temp = new TextButton("Exit", ColorRGBA.black, 40);
		temp.setAlignment(Alignment.CENTERED);
		temp.print("Exit");
		temp.setState("MouseOut", ColorRGBA.black);
		temp.setState("MouseOver", ColorRGBA.white);
		temp.setState("MouseClick", ColorRGBA.gray);
		buttons.put(temp.getName(), temp);
		defaultMenu.addElement(temp);
		
		return defaultMenu;
	}

	@Override
	public boolean onKey(int keyCode, boolean pressed) {
		return false;
	}

	@Override
	public boolean onMouseMove(int delta, int delta2, int x, int y) {
		Iterator<Button> it = buttons.values().iterator();
		boolean processed = false;
		
		while(it.hasNext()) {
			Button temp = it.next();
			if(temp.contains(x, y)) {
				if(!temp.isPressed()) {
					temp.onMouseOver();
					processed = true;
				}
			}
			else {
				if(!temp.isPressed()) {
					temp.onMouseOut();
				}
			}
		}
		
		return processed;
	}

	@Override
	public boolean onMouseClick(int button, int x, int y) {
		Iterator<Button> it = buttons.values().iterator();
		boolean processed = false;
		
		while(it.hasNext()) {
			Button temp = it.next();
			if(temp.contains(x, y)) {
				temp.onMouseClick();
				processed = true;
			}
			else {
				temp.onMouseOut();
			}
		}
		
		return processed;
	}

	@Override
	public boolean onMouseRelease(int button, int x, int y) {
		Iterator<Button> it = buttons.values().iterator();
		boolean processed = false;
		
		while(it.hasNext()) {
			Button temp = it.next();
			if(temp.contains(x, y)) {
				if(temp.isPressed() && temp.getSpatial().getCullHint() != CullHint.Always) {
					temp.onMouseRelease();
					doAction(temp.getName());
					processed = true;
				}
			}
			else {
				if(temp.isPressed()) {
					temp.onMouseRelease();
				}
				temp.onMouseOut();
			}
		}
		
		return processed;
	}
	
	@Override
	public boolean onWheel(int wheelDelta, int x, int y) {
		boolean processed = false;

		return processed;
	}
	
	public void doAction(String action) {
		if(action.compareTo("New Game") == 0) {
			GameApp.manager.stateChange("New Campaign");
		}
		else if(action.compareTo("Exit") == 0) {
			GameStateManager.getInstance().cleanup();
			GameApp.game.shutdown();
		}
	}

	@Override
	public void cleanup() {

	}

	@Override
	public void render(float tpf) {
		DisplaySystem.getDisplaySystem().getRenderer().draw(guiRoot.getSpatial());
	}
	
	@Override
	public void update(float tpf) {
		guiRoot.getSpatial().updateGeometricState(tpf, true);
	}
}
